package com.bookm.bookm.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.bookm.bookm.entity.Book;
import com.bookm.bookm.entity.Reader;
import com.bookm.bookm.entity.RecorderVO;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.RecorderService;
import com.bookm.bookm.utils.EntityUtil;
import com.bookm.bookm.utils.ResultUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CancellationException;

public class RecorderServiceImpl implements RecorderService {
    @Override
    public Result getRecordList() {
        ArrayList<RecorderVO> recorderVOS = new ArrayList<>();
        int i = 0;
        String maxBorrowDays;
        EntityUtil entityUtil = new EntityUtil();
        ConfigurationserviceImpl configurationService = new ConfigurationserviceImpl();
        maxBorrowDays=configurationService.getval("maxBorrowdays");
        i = Integer.parseInt(maxBorrowDays);
        try {
            List<Entity> entityList = Db.use().query("select book.book_id,book_name,reader.id,name,recorder.rd_date,college,professional from book,recorder,reader where book.book_id = recorder.book_id and reader.id = recorder.id");
            for(Entity entity: entityList){
                RecorderVO recorderVO = entityUtil.toObj(new RecorderVO(),entity);
                Calendar instance = Calendar.getInstance();
                instance.setTime(recorderVO.getRdDate());
                instance.add(Calendar.DAY_OF_YEAR, i);
                Date time = instance.getTime();
                recorderVO.setEndDate(time);
                recorderVOS.add(recorderVO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultUtil.success(recorderVOS);
    }

    @Override
    public Result getReaderRecordList(String id) {
        ArrayList<RecorderVO> recorderVOS = new ArrayList<RecorderVO>();
        int i = 0;
        String maxBorrowDays;
        EntityUtil entityUtil = new EntityUtil();
        ConfigurationserviceImpl configurationService = new ConfigurationserviceImpl();
        maxBorrowDays=configurationService.getval("maxBorrowdays");
        i = Integer.parseInt(maxBorrowDays);
        try {
            List<Entity> entityList = Db.use().query("select book.book_id,book_name,reader.id,name,recorder.rd_date,college,professional from book,recorder,reader where book.book_id = recorder.book_id and reader.id = recorder.id and reader.id = ?",id);
            for(Entity entity: entityList){
                RecorderVO recorderVO = entityUtil.toObj(new RecorderVO(),entity);
                Calendar instance = Calendar.getInstance();
                instance.setTime(recorderVO.getRdDate());
                instance.add(Calendar.DAY_OF_YEAR, i);
                Date time = instance.getTime();
                recorderVO.setEndDate(time);
                recorderVOS.add(recorderVO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultUtil.success(recorderVOS);
    }

    @Override
    public Result addRecord(String id, String bookId) {
        int i=0;
        BookServiceImpl bookService = new BookServiceImpl();
        Book book = bookService.getBook(bookId);
        ReaderServiceImpl readerService = new ReaderServiceImpl();
        Result reader = readerService.getUsr(id);
        RecorderServiceImpl recorderService = new RecorderServiceImpl();
        Result result = recorderService.getReaderRecordList(id);
        if(book == null){
            Result failure = ResultUtil.failure();
            failure.setMsg("该书不存在");
            return failure;
        }
        if(reader.getData() == null){
            Result failure = ResultUtil.failure();
            failure.setMsg("该用户不存在");
            return  failure;
        }
        if(result.getData() != null){
            List<RecorderVO> data = (List<RecorderVO>) result.getData();
            ConfigurationserviceImpl configurationservice = new ConfigurationserviceImpl();
            String result1  = configurationservice.getval("maxBorrownum");
            int num = Integer.parseInt(result1);
            if(data.size() >= num){
                Result failure = ResultUtil.failure();
                failure.setMsg("已借阅最大本数");
                return  failure;
            }
        }
        Date rdDate = new Date();
        try {
            i = Db.use().insert(Entity.create("recorder").set("id",id).set("book_id",bookId).set("rd_date",rdDate));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i == 1){
            return ResultUtil.success(i);
        }else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result delRecord(String id, String bookId) {
        int i=0;
        BookServiceImpl bookService = new BookServiceImpl();
        Book book = bookService.getBook(bookId);
        ReaderServiceImpl readerService = new ReaderServiceImpl();
        Result reader = readerService.getUsr(id);
        if(book == null){
            Result failure = ResultUtil.failure();
            failure.setMsg("该书不存在");
            return failure;
        }
        if(reader.getData() == null){
            Result failure = ResultUtil.failure();
            failure.setMsg("该用户不存在");
            return  failure;
        }
        try {
            i = Db.use().del(Entity.create("recorder").set("id",id).set("book_id",bookId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i == 1){
            return ResultUtil.success(i);
        }else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result updateRecord(String id, String bookId) {
        int i = 0;
        Date rdDate = new Date();
        try {
            i = Db.use().update(Entity.create().set("rd_date",rdDate),Entity.create("recorder").set("id",id).set("book_id",bookId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i == 1){
            return ResultUtil.success(i);
        }else {
            return ResultUtil.failure();
        }
    }
}
